'use strict';
/**
 * Created by FreezeSoul on 2015/10/10.
 */
define([
        'angular',
        'adConfig',
        'app/modules/folder/define'
    ],
    function (angular, adConfig) {
        angular.module('ad.folder')
            .factory('adFolderEditService', ['$http', '$q', '_', 'adRequest', function ($http, $q, _, adRequest) {

                function getFolders(folderType) {
                    var deferred = $q.defer();
                    var url = adConfig.baseServiceUrl + 'common/folder/get';
                    adRequest(url, {
                        folderType: folderType
                    }).success(function (data, status, headers, config) {
                        deferred.resolve(data);
                    }).error(function (data, status, headers, config) {
                        deferred.reject(status);
                    });
                    return deferred.promise;
                }

                function addFolder(folderName, folderParent, folderType) {
                    var deferred = $q.defer();
                    var url = adConfig.baseServiceUrl + 'common/folder/add';
                    adRequest(url, {
                        folderName: folderName,
                        folderType: folderType,
                        folderParent: folderParent
                    }).success(function (data, status, headers, config) {
                        deferred.resolve(data);
                    }).error(function (data, status, headers, config) {
                        deferred.reject(status);
                    });
                    return deferred.promise;
                }

                function updateFolder(folderId, folderName, folderType) {
                    var deferred = $q.defer();
                    var url = adConfig.baseServiceUrl + 'common/folder/update';
                    adRequest(url, {
                        folderId: folderId,
                        folderName: folderName,
                        folderType: folderType
                    }).success(function (data, status, headers, config) {
                        deferred.resolve(data);
                    }).error(function (data, status, headers, config) {
                        deferred.reject(status);
                    });
                    return deferred.promise;
                }


                function removeFolder(folderId, folderType) {
                    var deferred = $q.defer();
                    var url = adConfig.baseServiceUrl + 'common/folder/delete';
                    adRequest(url, {
                        folderId: folderId,
                        folderType: folderType
                    }).success(function (data, status, headers, config) {
                        deferred.resolve(data);
                    }).error(function (data, status, headers, config) {
                        deferred.reject(status);
                    });
                    return deferred.promise;
                }

                return {
                    getFolders: getFolders,
                    addFolder: addFolder,
                    updateFolder: updateFolder,
                    removeFolder: removeFolder
                };
            }]);
    });